﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DTO;
using BUS;

namespace TimKiemDichVu
{
    public partial class MainPage : PhoneApplicationPage
    {
        private List<DuLieuDTO> duLieu;
        private List<DuLieuThoDTO> duLieuTho;
        private SearchEngine SEngine;

        public MainPage()
        {
            InitializeComponent();
            (Application.Current as App).define = new Define();
            (Application.Current as App).service = new Service();
            (Application.Current as App).district = new District();            
            (Application.Current as App).type = 0;
            SEngine  = new SearchEngine();
        }			

		#region Process events
		#region Update:
		private void tblUpdate_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			// TODO: Add event handler implementation here.
            
		}


		private void tblUpdate_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			// TODO: Add event handler implementation here.	            
            NavigationService.Navigate(new Uri("/UpdatePage.xaml", UriKind.Relative));		
		}
		
		private void tblUpdate_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
		{
			// TODO: Add event handler implementation here.
		}

		private void tblUpdate_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
		{
			// TODO: Add event handler implementation here.
		}
		#endregion
		
		#region Help:
		private void tblHelp_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			// TODO: Add event handler implementation here.
		}

		private void tblHelp_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			// TODO: Add event handler implementation here.
            NavigationService.Navigate(new Uri("/HelpPage.xaml", UriKind.Relative));
		}
		
		private void tblHelp_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
		{
			// TODO: Add event handler implementation here.
		}

		private void tblHelp_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
		{
			// TODO: Add event handler implementation here.
		}
		#endregion
		
		#region About:
		private void tblAbout_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			// TODO: Add event handler implementation here.			
		}
		
		private void tblAbout_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			// TODO: Add event handler implementation here.
            NavigationService.Navigate(new Uri("/AboutPage.xaml", UriKind.Relative));
		}

		private void tblAbout_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
		{
			// TODO: Add event handler implementation here.
		}

		private void tblAbout_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
		{
			// TODO: Add event handler implementation here.
		}
		#endregion
		
		#region Search:        
        private void AddItem()
        {
            ltbResult.Items.Clear();

            for (int i = 0; i < duLieuTho.Count; i++ )
            {
                uscLocation item = new uscLocation(duLieuTho[i]);
                ltbResult.Items.Add(item);
            }

            tblNumberPage.Text = ltbResult.Items.Count.ToString() + " Địa điểm";
        }

		private void btnSearch_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			// TODO: Add event handler implementation here. 
            if (tbxInput.Text != "")
            {
                string strKey = tbxInput.Text;                
                               
                //Goi Ham Search trong SearchEngine
                duLieu = SEngine.Search(strKey, 0);
                duLieuTho = SEngine.Reference(duLieu);
                AddItem();
            }
            else
            {
                MessageBox.Show("Chưa nhập từ khóa!");
            }
		}
        #endregion

        #region Choose Service:
        private void tblServiceName_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			// TODO: Add event handler implementation here.			
		}

		private void tblServiceName_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			// TODO: Add event handler implementation here.
            NavigationService.Navigate(new Uri("/ChooseServicePage.xaml", UriKind.Relative));
		}

		private void tblServiceName_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
		{
			// TODO: Add event handler implementation here.
		}

		private void tblServiceName_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
		{
			// TODO: Add event handler implementation here.
		}
		#endregion

        #region On navigated to:
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            switch ((Application.Current as App).type)
            {
                case 0:
                    break;
                case 1:
                    if ((Application.Current as App).district.IndexList.Count == 1)
                    {
                        int i = (Application.Current as App).district.IndexList[0];
                        tblDistrictName.Text = (Application.Current as App).define.DistrictNameList[i];
                    }
                    else if ((Application.Current as App).district.IndexList.Count > 1)
                    {
                        tblDistrictName.Text = "Nhiều Quận, Huyện";
                    }
                    break;
                case 2:
                    if ((Application.Current as App).service.IndexList.Count == 1)
                    {
                        int i = (Application.Current as App).service.IndexList[0];
                        tblServiceName.Text = (Application.Current as App).define.ServiceNameList[i];
                    }
                    else if ((Application.Current as App).service.IndexList.Count > 1)
                    {
                        tblServiceName.Text = "Nhiều Dịch vụ";
                    }                    
                    break;
                default:
                    break;
            }
            base.OnNavigatedTo(e);
        }
        #endregion

        #region Choose District:
        private void tblDistrictName_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			// TODO: Add event handler implementation here.
		}

		private void tblDistrictName_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			// TODO: Add event handler implementation here.            
            NavigationService.Navigate(new Uri("/ChooseDistrictPage.xaml", UriKind.Relative));                       
		}

		private void tblDistrictName_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
		{
			// TODO: Add event handler implementation here.
		}

		private void tblDistrictName_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
		{
			// TODO: Add event handler implementation here.
		}
		#endregion

		#region Watch location details:
		private void btnLocationDetail_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			// TODO: Add event handler implementation here.
			btnLocationDetail.Fill = (ImageBrush)Main.Resources["ButtonLocationDetailSelected"];
		}

		private void btnLocationDetail_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			// TODO: Add event handler implementation here.
			btnLocationDetail.Fill = (ImageBrush)Main.Resources["ButtonLocationDetail"];
            if (ltbResult.SelectedItems.Count > 0)
            {
                int iIndex = ltbResult.SelectedIndex;
                uscLocation usc = (uscLocation)(ltbResult.Items[iIndex]);
                (Application.Current as App).location = usc.location;
                NavigationService.Navigate(new Uri("/LocationPage.xaml", UriKind.Relative));
            }
		}
		
		private void btnLocationDetail_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
		{
			// TODO: Add event handler implementation here.
			btnLocationDetail.Fill = (ImageBrush)Main.Resources["ButtonLocationDetail"];
		}
		
		private void btnLocationDetail_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
		{
			// TODO: Add event handler implementation here.
			btnLocationDetail.Fill = (ImageBrush)Main.Resources["ButtonLocationDetailSelected"];
		}
		#endregion			
		#endregion
    }
}
